/**
 * виджет всплывающей подсказки
 */

;(function($){

  $.widget("ui.jtip", {
    options: {url: "", urlAttr: "href", title: null, corner: true, width: 250},
  // конструктор
    _create: function()
             {
               $.extend(this.options, {
                 title: this.options.title || this.element.attr("title"),
                 width: parseInt(this.options.width)
               });
               var self = $.extend(this, {
                 _show: function()
                        {
                          var $this = $(this);
                          var url   = self.options.url || $this.attr(self.options.urlAttr);
                          if (url)
                          {
                          // Get an object left position from the upper left viewport corner
                            var parent = this;
                            var pos    = {left: parent.offsetLeft, top: parent.offsetTop};
                            var p;
                          // Get left position from the parent object
                          // Parse the parent hierarchy up to the document element
                            while (parent.offsetParent)
                            {
                            // Get parent object reference
                              p         = parent.offsetParent;
                            // Add parent left position
                              pos.left += p.offsetLeft;
                              parent    = p;
                            }
                            var d         = document.documentElement;
                            var rightSide = (self.options.width + 75) < ((this.innerWidth || (d && d.clientWidth) || document.body.clientWidth) - pos.left);
                            pos.left     += rightSide ? 12 + this.offsetWidth : -(15 + self.options.width);
                          // Get an object top position from the upper left viewport corner
                            parent = this;
                          // Get top position from the parent object
                          // Parse the parent hierarchy up to the document element
                            while (parent.offsetParent)
                            {
                            // Get parent object reference
                              p        = parent.offsetParent;
                            // Add parent top position
                              pos.top += p.offsetTop;
                              parent   = p;
                            }
                            pos.width = self.options.width;
                            $.ui.jtip.hide();
                            $.ui.jtip.tip.
                              toggleClass(["ui-corner-bottom ui-corner-t", rightSide ? "r" : "l"].join(""), self.options.corner).
                              css(pos).
                              children().
                              filter(".jt-arrow").
                              toggleClass("jt-arrow-left", rightSide).
                              toggleClass("jt-arrow-right", !rightSide).
                              end().
                              filter(".jt-arrow-right").css("left", pos.width + 1);
                            $.ui.jtip.show(self.options.title || $this.text(), url);
                          }
                        }
               });
               this.element.attr("title", "").hover(this._show, $.ui.jtip.hide);
             },
  // деструктор
    destroy: function()
             {
               $.ui.jtip.hide();
               this.element.unbind("leave", $.ui.jtip.hide).unbind("enter", this._show);
             }
  });

  $.extend($.ui.jtip, {
    tip : $("<div />", {
            "class": "jtip",
            "style": "position:absolute;display:none;z-index:100;",
            "html" : "<div class='jt-arrow' style='position:absolute;z-index:101;' /><div class='jt-title' /><div class='jt-content'><div class='jt-loader' /></div>"
          }).appendTo(document.body),
    show: function(title, url)
          {
            if (url)
              $.ui.jtip.tip.children(".jt-title").html(title || "").end().show().children(".jt-content").load(url);
          },
    hide: function(){$.ui.jtip.tip.children().filter(".jt-title").empty().end().filter(".jt-content").html("<div class='jt-loader' />").end().end().hide();}
  });

})(jQuery);
